<?php

/**
 * Generate link to public resource from within a module
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Resource.php 38 2009-02-18 17:12:39Z monzee $
 */
class Crud_Resource
{
    /**
     * Generate link
     *
     * @param string $resource Resource name
     * @return string
     */
    public function resource($resource)
    {
        $resource = ltrim($resource, '/');

        $front = Zend_Controller_Front::getInstance();
        $request = $front->getRequest();
        $baseUrl = $request->getBaseUrl();
        $module = $request->getModuleName();

        if ($front->hasPlugin('Mz_Module_Initializer')) {
            $plugin = $front->getPlugin('Mz_Module_Initializer');
            $module = $plugin->getModule($module);
            
            return $module->urlToResource($resource, $baseUrl);
        }
        
        return $baseUrl . '/' . $resource;
    }

}
